package com.seesman.application.maintenance.common.gui;

import static com.seesman.application.maintenance.common.gui.utilities.MessageFormatUtil.format;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;

import com.seesman.application.maintenance.common.gui.utilities.FrameUtil;
import com.seesman.application.maintenance.common.gui.utilities.WindowUtilities;
import com.seesman.application.maintenance.gui.certificate.CertificateFrame;

public class SeesmanMainWindow extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 3099513023859694218L;

	private static SeesmanMainWindow window;
	
	private static final String resourceBundle = "SeesmanMainWindow";
	
	private String name;
	
	//[START] - Variable Declaration
	private JDesktopPane desktop;
	private JMenuBar menuBar;
	private JMenu menuFile;
	private JMenuItem menuFileItemNew;
	private JMenuItem menuFileItemSave;
	private JMenuItem menuFileItemSearch;
	private JMenuItem menuFileItemPrintPreview;
	private JMenuItem menuFileItemPrint;
	private JMenuItem menuFileItemExit;	
	private JMenu menuEdit;
	private JMenu menuMaintenance;
	private JMenu menuTools;
	private JMenu menuWindows;
	private JMenu menuHelp;
	private JMenuItem menuHelpItemHelp;
	private JMenuItem menuHelpItemAbout;
	//[END] - Variable Declaration
	
	/**
	 * Constructor Class
	 * 
	 * Set as private to make sure that nobody will 
	 * create an instance of this class
	 * 
	 */
	private SeesmanMainWindow(String name) {        
		this.name = name;
    }
	
	/**
	 * This method make sure that there is only one instance
	 * for this class
	 * 
	 * @param name
	 * @return
	 */
	public static SeesmanMainWindow getInstance(String name){
        if(window == null){
            window = new SeesmanMainWindow(name);   
            window.build("JHAYNE");
        }
        return window;
    }
		
	/** 
     * This method is called from within the constructor to
     * initialize the form. 
     */	
	private void build(String title){
		WindowUtilities.setNativeLookAndFeel();	
		
		window.setLocationRelativeTo(null);
		window.setContentPane(buildContentPane());
		window.setTitle("SEESMAN - " + title);
		window.setJMenuBar(buildMenuBar());
		window.setIconImage(new ImageIcon("icon-image/save.gif").getImage());
		window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		window.setSize(400, 300);		
		window.add(buildSplitPane());
	}
	
	private JComponent buildSplitPane(){
		desktop = new JDesktopPane();
		JLabel label = new JLabel("SAMPLE ONLY");	
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, label, desktop);		
		splitPane.setDividerLocation(150);		
		
		return splitPane;
	}
	
	/**
	 * 
	 * @return
	 */
	private JComponent buildContentPane(){		
		JPanel panel = new JPanel(new BorderLayout());
        panel.add(buildToolBar(), BorderLayout.NORTH);
        return panel;
	}
	
	
	/**
	 * 
	 * @return
	 */
	private Component buildToolBar(){
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.putClientProperty("JToolBar.isRollover",Boolean.TRUE);
		toolBar.add(new JButton("SAMPLE"));
		toolBar.addSeparator();
		toolBar.add(new JButton("SAMPLE2"));
		return toolBar;
	}
	
	/**
	 * 
	 * @return
	 */
	private JMenuBar buildMenuBar(){
		JMenuBar menuBar = new JMenuBar();
		
		FrameUtil util = new FrameUtil();		
		
		menuBar = new JMenuBar();
		menuFile = new JMenu(format(resourceBundle, "menu.File"));
		menuFile.setMnemonic(KeyEvent.VK_F);		
		
			menuFileItemNew = new JMenuItem(format(resourceBundle, "menu.item.New"));
			menuFileItemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
			menuFileItemNew.addActionListener(this);
			menuFile.add(menuFileItemNew);
			
			menuFile.add(new JSeparator());
			
			menuFileItemSave = new JMenuItem(format(resourceBundle, "menu.item.Save"));			
			menuFileItemSave.setIcon(util.readImageIcon(format(resourceBundle, "icon.Save")));
			menuFile.add(menuFileItemSave);
			
			menuFileItemSearch = new JMenuItem(format(resourceBundle, "menu.item.Search"));
			menuFile.add(menuFileItemSearch);
			
			menuFile.add(new JSeparator());
			
			menuFileItemPrintPreview = new JMenuItem(format(resourceBundle, "menu.item.PrintPreview"));
			menuFile.add(menuFileItemPrintPreview);
			
			menuFileItemPrint = new JMenuItem(format(resourceBundle, "menu.item.Print"));
			menuFile.add(menuFileItemPrint);
			
			menuFile.add(new JSeparator());
			
			menuFileItemExit = new JMenuItem(format(resourceBundle, "menu.item.Exit"));
			menuFileItemExit.setMnemonic(KeyEvent.VK_X);
			menuFileItemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
			menuFileItemExit.addActionListener(this);
			menuFile.add(menuFileItemExit);			
			
			menuBar.add(menuFile);
		
		menuEdit = new JMenu(format(resourceBundle, "menu.Edit"));
		menuEdit.setMnemonic(KeyEvent.VK_E);
			
			menuBar.add(menuEdit);
		
		menuMaintenance = new JMenu(format(resourceBundle, "menu.Maintenance"));
		menuMaintenance.setMnemonic(KeyEvent.VK_M);
		
			menuBar.add(menuMaintenance);
		
		menuTools = new JMenu(format(resourceBundle, "menu.Tools"));
		menuTools.setMnemonic(KeyEvent.VK_T);
		
			menuBar.add(menuTools);	
		
		menuWindows = new JMenu(format(resourceBundle, "menu.Window"));
		menuWindows.setMnemonic(KeyEvent.VK_W);
		
			menuBar.add(menuWindows);	
		
		menuHelp = new JMenu(format(resourceBundle, "menu.Help"));
		menuHelp.setMnemonic(KeyEvent.VK_H);
			
			menuHelpItemHelp = new JMenuItem(format(resourceBundle, "menu.item.Help"));
			menuHelpItemHelp.setMnemonic(KeyEvent.VK_H);
			menuHelp.add(menuHelpItemHelp);
			
			menuHelpItemAbout = new JMenuItem(format(resourceBundle, "menu.item.About"));
			menuHelpItemAbout.setMnemonic(KeyEvent.VK_A);
			menuHelpItemAbout.addActionListener(this);
			menuHelp.add(menuHelpItemAbout);
			
			menuBar.add(menuHelp);
			
		return menuBar;
	}
	
	/**
	 * 
	 */
	public void actionPerformed(ActionEvent event) {
		Object source = event.getSource();
		
		if (source == menuFileItemNew){	
			desktop.add(new CertificateFrame()); 
		}
		
		if (source == menuHelpItemAbout){	
			new SeesmanAboutBox(window); 
		}
		
		if (source == menuFileItemExit){	
            System.exit(0); 
		}
				
	}
		
}
